package com.google.code.sprite.conf;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import java.io.File;
import java.io.IOException;
import java.io.FileReader;

public class DbConfig {

    private String pwd;
    private String user;
    private String url;
    private String driver;

        
    private DbConfig(String pwd, String user, String url, String driver) {
        this.pwd = pwd;
        this.user = user;
        this.url = url;
        this.driver = driver;
    }

    private static DbConfig loadConfig(String filePath ) {
        Properties prop = new Properties();
        try {
            FileReader fr = new FileReader(new File(filePath));
            prop.load(fr);
            String pwd = prop.getProperty("connection.password");
            String user  = prop.getProperty("connection.username");
            String url = prop.getProperty("connection.url");
            String driver = prop.getProperty("connection.driver");
            DbConfig dbconfig = new DbConfig(pwd,user,url,driver);
            return dbconfig;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Connection getConnect() {
        try {
            DbConfig app = DbConfig.loadConfig("db.properties");
            Class.forName(app.getDriver());
            Connection conn = DriverManager.getConnection(app.getUrl(), app.getUser(), app.getPwd());
            return conn;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    public void setPwd(String pwd) {
        this.pwd=pwd;
    }
    public String getPwd() {
        return this.pwd;
    }


    public void setUser(String user) {
        this.user=user;
    }
    public String getUser() {
        return this.user;
    }


    public void setUrl(String url) {
        this.url=url;
    }
    public String getUrl() {
        return this.url;
    }


    public void setDriver(String driver) {
        this.driver=driver;
    }
    public String getDriver() {
        return this.driver;
    }

}
